package com.kjzy.controller;

import com.github.pagehelper.PageInfo;
import com.kjzy.pojo.Devices;
import com.kjzy.pojo.Member;
import com.kjzy.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class MemberController {
    @Autowired
    private MemberService memberService;
    @RequestMapping("/member_manage")
    public String toMemberManage(){
        return "admin/member_manage";
    }
    @RequestMapping("listMember.action")
    public String list(Member member,
                             @RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "10") int pageSize,
                             Model model){
        PageInfo<Member> pageInfo =memberService.findAllM(member,pageNum,pageSize);

        //3. 将分页信息存入Model中，供视图使用
        model.addAttribute("memberList", pageInfo.getList());
        model.addAttribute("pageInfo", pageInfo);

        //4. 返回待跳转的逻辑视图名。本例中，将跳转
        // 到"/WEB-INF/jsp/admin/user_manage.jsp"。
        return "admin/member_manage";
    }
    @RequestMapping(value = "/createMember.action", method = RequestMethod.POST)
    @ResponseBody
    public String createMember(@RequestParam("name") String name,
                               @RequestParam("tel") String tel,
                               @RequestParam("address") String address,
                               @RequestParam("grade") String grade) throws Exception{
        Member member=new Member();
        member.setName(name);
        member.setTel(tel);
        member.setAddress(address);
        member.setGrade(grade);
        String result=memberService.createMember(member);
        return result;
    }
    @RequestMapping("/findMember.action")
    @ResponseBody
    public Member findMember(@RequestParam("id")Integer id){
        Member member=memberService.findById(id);
        return member;
    }
    @RequestMapping("/updateMember.action")
    @ResponseBody
    public String updateMember(@RequestBody Member member){
        String result=memberService.updateMember(member);
        return result;
    }
    @RequestMapping("/deleteMember.action")
    @ResponseBody
    public String deleteMember(@RequestParam("id") Integer id){
        String result = memberService.deleteMember(id);
        // 3. 返回(删除用户的)结果，不是跳转到新页面。
        // 本例中，返回的不是一段JSON数据，就是一个普通的String数据("SUCCESS"
        // 或错误提示信息)。 但是，需要在方法上添加@ResponseBody注解。如果不添加该
        // 注解，SpringMVC会将待返回的String数据，误认为是待跳转新页面的路径值，即
        // 会误认为要跳转到/WEB-INF/jsp/SUCCESS.jsp或其它jsp页面。
        return result;
    }
}
